Heatmaps for Correlation
Heatmap for Correlation by \(R^2\)
- The correlation matrix of \(R^2\) is calculated by linear regression or Cramér’s V.
- The matrix is presented using R plotly package.
library(data.table)
dt <- data.table(
var1 = 1:20 + rnorm(20)
, var2 = 1:20 + rnorm(20) * 2
, var3 = 1:20 + rnorm(20) * 4
, var4 = 1:20 + rnorm(20) * 8
, var5 = 1:20 + rnorm(20) * 16
, var6 = sample(c("A", "B"), size = 20, replace = TRUE)
, var7 = sample(c("I", "II", "III"), size = 20, replace = TRUE)
)
Wu::heatmap(dt)Heatmap for Spearman’s Correlation Coefficient
- Use ggcorrplot package to present the heatmap of a correlation matrix of Spearman’s \(\rho\)
library(ggcorrplot)
library(data.table)
dt <- data.table(
var1 = 1:20 + rnorm(20)
, var2 = 1:20 + rnorm(20) * 2
, var3 = 1:20 + rnorm(20) * 4
, var4 = 1:20 + rnorm(20) * 8
, var5 = 1:20 + rnorm(20) * 16
)
NVars <- c("var1"
, "var2"
, "var3"
, "var4"
, "var5"
)
mtx <- cor(dt[, ..NVars]
## , na.rm=FALSE
, use="na.or.complete"
, method="spearman")
ggcorrplot(mtx,
hc.order = FALSE,
outline.color = "white",
type = "lower",
## colors = c("red", "white", "blue"),
show.diag = TRUE,
lab = TRUE
, tl.srt=30
, lab_size=10
, tl.cex=10
, show.legend = FALSE
## , ggtheme = ggplot2::theme_gray
, colors = c("#6D9EC1", "white", "#E46726")
## , method="circle"
)R sessionInfo
R version 4.1.2 (2021-11-01) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.3 LTS
Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale: [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] ggcorrplot_0.1.3 Wu_0.0.0.9000 flexdashboard_0.5.2 [4] lme4_1.1-27.1 Matrix_1.4-0 mgcv_1.8-38
[7] nlme_3.1-152 png_0.1-7 scales_1.1.1
[10] nnet_7.3-16 labelled_2.8.0 kableExtra_1.3.4
[13] plotly_4.9.4.1 gridExtra_2.3 ggplot2_3.3.5
[16] DT_0.18 tableone_0.13.0 magrittr_2.0.3
[19] lubridate_1.7.10 dplyr_1.0.9 plyr_1.8.6
[22] data.table_1.14.2 rmdformats_1.0.2 knitr_1.39
loaded via a namespace (and not attached): [1] httr_1.4.2 sass_0.4.0 tidyr_1.1.3 jsonlite_1.7.2
[5] viridisLite_0.4.0 splines_4.1.2 bslib_0.2.5.1 assertthat_0.2.1
[9] highr_0.9 yaml_2.3.5 pillar_1.7.0 lattice_0.20-45
[13] glue_1.6.2 digest_0.6.29 RColorBrewer_1.1-2 rvest_1.0.0
[17] minqa_1.2.4 colorspace_2.0-2 htmltools_0.5.2 survey_4.0
[21] pkgconfig_2.0.3 haven_2.4.1 bookdown_0.22 purrr_0.3.4
[25] webshot_0.5.2 svglite_2.0.0 tibble_3.1.7 farver_2.1.0
[29] generics_0.1.2 ellipsis_0.3.2 withr_2.4.3 klippy_0.0.0.9500 [33] lazyeval_0.2.2 cli_3.3.0 survival_3.2-13 crayon_1.5.1
[37] evaluate_0.15 fansi_1.0.3 MASS_7.3-54 forcats_0.5.1
[41] xml2_1.3.3 tools_4.1.2 hms_1.1.0 mitools_2.4
[45] lifecycle_1.0.1 stringr_1.4.0 munsell_0.5.0 compiler_4.1.2
[49] jquerylib_0.1.4 systemfonts_1.0.2 rlang_1.0.2 grid_4.1.2
[53] nloptr_1.2.2.2 rstudioapi_0.13 htmlwidgets_1.5.4 crosstalk_1.1.1
[57] rmarkdown_2.10 boot_1.3-28 gtable_0.3.0 DBI_1.1.1
[61] reshape2_1.4.4 R6_2.5.1 fastmap_1.1.0 utf8_1.2.2
[65] stringi_1.7.6 Rcpp_1.0.8.3 vctrs_0.4.1 tidyselect_1.1.2
[69] xfun_0.31